package com.zhiyou.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.github.pagehelper.util.PageObjectUtil;
import com.xinchao.component.multids.util.PageInfoUtil;
import com.zhiyou.app.common.constant.UserHandler;
import com.zhiyou.app.pojo.entity.UserWalletEntity;
import com.zhiyou.app.pojo.entity.WalletHistoryEntity;
import com.zhiyou.app.mapper.WalletHistoryMapper;
import com.zhiyou.app.pojo.enums.WalletTypeEnum;
import com.zhiyou.app.pojo.vo.UserWalletVo;
import com.zhiyou.app.pojo.vo.WalletDetailRequestVo;
import com.zhiyou.app.pojo.vo.WalletDetailVo;
import com.zhiyou.app.service.IUserWalletService;
import com.zhiyou.app.service.IWalletHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jonathan.z
 * @since 2025-07-03
 */
@Service
public class WalletHistoryServiceImpl extends ServiceImpl<WalletHistoryMapper, WalletHistoryEntity> implements IWalletHistoryService {
    @Value("${wallet.com-id:1}")
    private Integer comWalletId;
    @Resource
    private IUserWalletService userWalletService;

    @Override
    public PageInfo<WalletDetailVo> getWalletHistory(WalletDetailRequestVo walletDetailRequestVo) {
        LambdaQueryWrapper<WalletHistoryEntity> eq = new LambdaQueryWrapper<WalletHistoryEntity>().eq(WalletHistoryEntity::getWalletType, walletDetailRequestVo.getWalletType());
        PageMethod.startPage(walletDetailRequestVo.getPageNum(), walletDetailRequestVo.getPageSize());
        if (walletDetailRequestVo.getWalletType() == WalletTypeEnum.USER) {
            if (walletDetailRequestVo.getUserId() == null) {
                walletDetailRequestVo.setUserId(UserHandler.getLoginUser().getUserId());
            }
            eq.eq(WalletHistoryEntity::getUserId, UserHandler.getLoginUser().getUserId());
        } else {
            eq.ge(WalletHistoryEntity::getWalletId, comWalletId);
        }
        if (walletDetailRequestVo.getMinAmount() != 0) {
            eq.ge(WalletHistoryEntity::getAmount, walletDetailRequestVo.getMinAmount());
        }
        if (walletDetailRequestVo.getMaxAmount() != 0) {
            eq.le(WalletHistoryEntity::getAmount, walletDetailRequestVo.getMaxAmount());
        }
        if (walletDetailRequestVo.getMinTradingTime() != null) {
            eq.ge(WalletHistoryEntity::getCreateTime, walletDetailRequestVo.getMinTradingTime());
        }
        if (walletDetailRequestVo.getMaxTradingTime() != null) {
            eq.le(WalletHistoryEntity::getCreateTime, walletDetailRequestVo.getMaxTradingTime());
        }


        PageInfo<WalletHistoryEntity> pageInfo = new PageInfo<>(list(eq.orderByDesc(WalletHistoryEntity::getCreateTime)));
        List<WalletHistoryEntity> list = pageInfo.getList();
        PageInfo<WalletDetailVo> newPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, newPageInfo);
        newPageInfo.setList(list.stream().map(t -> {
            WalletDetailVo walletDetailVo = new WalletDetailVo();
            walletDetailVo.setAmount(t.getAmount());
            walletDetailVo.setContent(t.getContent());
            walletDetailVo.setId(t.getId());
            walletDetailVo.setTradingTime(t.getCreateTime());
            walletDetailVo.setWalletId(t.getWalletId());
            return walletDetailVo;
        }).collect(Collectors.toList()));
        return newPageInfo;
    }
}
